package com.csii.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.csii.core.system.vo.LoginUser;
import com.csii.core.util.oConvertUtils;
import com.csii.modules.online.auth.service.IOnlAuthPageService;
import com.csii.modules.online.cgform.entity.OnlCgformButton;
import com.csii.modules.online.cgform.entity.OnlCgformEnhanceJs;
import com.csii.modules.online.cgform.entity.OnlCgformField;
import com.csii.modules.online.cgform.entity.OnlCgformHead;
import com.csii.modules.online.cgform.model.*;
import com.csii.modules.online.cgform.service.IOnlCgformFieldService;
import com.csii.modules.online.cgform.service.IOnlCgformHeadService;
import com.csii.modules.online.cgform.service.IOnlineService;
import com.csii.modules.online.cgform.table.TableComponent;
import com.csii.modules.online.cgform.util.EnhanceJsUtil;
import com.csii.modules.online.cgform.util.CgformSqlUtil;
import com.csii.modules.system.service.ISysBaseAPI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service("onlineService")
public class OnlineServiceImpl implements IOnlineService {
	@Autowired
	private IOnlCgformFieldService onlCgformFieldService;
	@Autowired
	private IOnlCgformHeadService onlCgformHeadService;
	@Autowired
	private ISysBaseAPI sysBaseAPI;
	@Autowired
	private IOnlAuthPageService onlAuthPageService;

	public OnlForeignKey.OnlComplexModel queryOnlineConfig(OnlCgformHead head) {
		String var2 = head.getId();
		List var3 = this.a(var2);
		List var4 = this.onlAuthPageService.queryHideCode(var2, true);
		ArrayList var5 = new ArrayList();
		HashMap var6 = new HashMap();
		ArrayList var7 = new ArrayList();
		ArrayList var8 = new ArrayList();
		ArrayList var9 = new ArrayList();
		Iterator var10 = var3.iterator();

		String var14;
		while (var10.hasNext()) {
			OnlCgformField var11 = (OnlCgformField) var10.next();
			String var12 = var11.getDbFieldName();
			String var13 = var11.getMainTable();
			var14 = var11.getMainField();
			if (oConvertUtils.isNotEmpty(var14) && oConvertUtils.isNotEmpty(var13)) {
				OnlForeignKey var15 = new OnlForeignKey(var12, var14);
				var8.add(var15);
			}

			if (1 == var11.getIsShowList() && !"id".equals(var12) && !var4.contains(var12) && !var9.contains(var12)) {
				OnlColumn var28 = new OnlColumn(var11.getDbFieldTxt(), var12);
				String var16 = var11.getDictField();
				String var17 = var11.getFieldShowType();
				if (oConvertUtils.isNotEmpty(var16) && !"popup".equals(var17)) {
					Object var18 = new ArrayList();
					if (oConvertUtils.isNotEmpty(var11.getDictTable())) {
						var18 = this.sysBaseAPI.queryTableDictItemsByCode(var11.getDictTable(), var11.getDictText(), var16);
					} else if (oConvertUtils.isNotEmpty(var11.getDictField())) {
						var18 = this.sysBaseAPI.queryDictItemsByCode(var16);
					}

					var6.put(var12, var18);
					var28.setCustomRender(var12);
				}

				List var29;
				if ("switch".equals(var17)) {
					var29 = CgformSqlUtil.fieldDicts(var11);
					var6.put(var12, var29);
					var28.setCustomRender(var12);
				}

				List var20;
				String var31;
				if ("link_down".equals(var17)) {
					var31 = var11.getDictTable();
					TableComponent var19 = (TableComponent) JSONObject.parseObject(var31, TableComponent.class);
					var20 = this.sysBaseAPI.queryTableDictItemsByCode(var19.getTable(), var19.getTxt(), var19.getKey());
					var6.put(var12, var20);
					var28.setCustomRender(var12);
					var5.add(var28);
					String var21 = var19.getLinkField();
					this.a(var3, var9, var5, var12, var21);
				}

				if ("sel_tree".equals(var17)) {
					String[] var33 = var11.getDictText().split(",");
					List var30 = this.sysBaseAPI.queryTableDictItemsByCode(var11.getDictTable(), var33[2], var33[0]);
					var6.put(var12, var30);
					var28.setCustomRender(var12);
				}

				if ("cat_tree".equals(var17)) {
					var31 = var11.getDictText();
					if (oConvertUtils.isEmpty(var31)) {
						String var32 = CgformSqlUtil.parseCode(var11.getDictField());
						var20 = this.sysBaseAPI.queryFilterTableDictInfo("SYS_CATEGORY", "NAME", "ID", var32);
						var6.put(var12, var20);
						var28.setCustomRender(var12);
					} else {
						var28.setCustomRender("_replace_text_" + var31);
					}
				}

				if ("sel_depart".equals(var17)) {
					var29 = this.sysBaseAPI.queryAllDepartBackDictModel();
					var6.put(var12, var29);
					var28.setCustomRender(var12);
				}

				if ("sel_user".equals(var11.getFieldShowType())) {
					var29 = this.sysBaseAPI.queryTableDictItemsByCode("SYS_USER", "REALNAME", "USERNAME");
					var6.put(var12, var29);
					var28.setCustomRender(var12);
				}

				if (var17.indexOf("file") >= 0) {
					var28.setScopedSlots(new ScopedSlots("fileSlot"));
				} else if (var17.indexOf("image") >= 0) {
					var28.setScopedSlots(new ScopedSlots("imgSlot"));
				} else if (var17.indexOf("editor") >= 0) {
					var28.setScopedSlots(new ScopedSlots("htmlSlot"));
				} else if (var17.equals("date")) {
					var28.setScopedSlots(new ScopedSlots("dateSlot"));
				} else if (var17.equals("pca")) {
					var28.setScopedSlots(new ScopedSlots("pcaSlot"));
				}

				if (StringUtils.isNotBlank(var11.getFieldHref())) {
					var31 = "fieldHref_" + var12;
					var28.setHrefSlotName(var31);
					var7.add(new HrefSlots(var31, var11.getFieldHref()));
				}

				if ("1".equals(var11.getSortFlag())) {
					var28.setSorter(true);
				}

				if (!"link_down".equals(var17)) {
					var5.add(var28);
				}
			}
		}

		OnlForeignKey.OnlComplexModel var22 = new OnlForeignKey.OnlComplexModel();
		var22.setCode(var2);
		var22.setTableType(head.getTableType());
		var22.setFormTemplate(head.getFormTemplate());
		var22.setDescription(head.getTableTxt());
		var22.setCurrentTableName(head.getTableName());
		var22.setPaginationFlag(head.getIsPage());
		var22.setCheckboxFlag(head.getIsCheckbox());
		var22.setScrollFlag(head.getScroll());
		var22.setColumns(var5);
		var22.setDictOptions(var6);
		var22.setFieldHrefSlots(var7);
		var22.setForeignKeys(var8);
		var22.setHideColumns(var4);
		List var23 = this.onlCgformHeadService.queryButtonList(var2, true);
		ArrayList var24 = new ArrayList();
		Iterator var25 = var23.iterator();

		while (var25.hasNext()) {
			OnlCgformButton var27 = (OnlCgformButton) var25.next();
			if (!var4.contains(var27.getButtonCode())) {
				var24.add(var27);
			}
		}

		var22.setCgButtonList(var24);
		OnlCgformEnhanceJs var26 = this.onlCgformHeadService.queryEnhanceJs(var2, "list");
		if (var26 != null && oConvertUtils.isNotEmpty(var26.getCgJs())) {
			var14 = EnhanceJsUtil.b(var26.getCgJs(), var23);
			var22.setEnhanceJs(var14);
		}

		if ("Y".equals(head.getIsTree())) {
			var22.setPidField(head.getTreeParentIdField());
			var22.setHasChildrenField(head.getTreeIdField());
			var22.setTextField(head.getTreeFieldname());
		}

		return var22;
	}

	private void a(List<OnlCgformField> var1, List<String> var2, List<OnlColumn> var3, String var4, String var5) {
		if (oConvertUtils.isNotEmpty(var5)) {
			String[] var6 = var5.split(",");
			String[] var7 = var6;
			int var8 = var6.length;

			for (int var9 = 0; var9 < var8; ++var9) {
				String var10 = var7[var9];
				Iterator var11 = var1.iterator();

				while (var11.hasNext()) {
					OnlCgformField var12 = (OnlCgformField) var11.next();
					String var13 = var12.getDbFieldName();
					if (1 == var12.getIsShowList() && var10.equals(var13)) {
						var2.add(var10);
						OnlColumn var14 = new OnlColumn(var12.getDbFieldTxt(), var13);
						var14.setCustomRender(var4);
						var3.add(var14);
						break;
					}
				}
			}
		}

	}

	public JSONObject queryOnlineFormObj(OnlCgformHead head, OnlCgformEnhanceJs onlCgformEnhanceJs) {
		JSONObject var3 = new JSONObject();
		String var4 = head.getId();
		List var5 = this.onlCgformFieldService.queryAvailableFields(head.getId(), head.getTableName(), (String) null, false);
		List var6 = this.onlAuthPageService.queryFormDisabledCode(var4);
		EnhanceJsUtil.a(onlCgformEnhanceJs, head.getTableName(), var5);
		OnlFieldModel var7 = null;
		if ("Y".equals(head.getIsTree())) {
			var7 = new OnlFieldModel();
			var7.setCodeField("id");
			var7.setFieldName(head.getTreeParentIdField());
			var7.setPidField(head.getTreeParentIdField());
			var7.setPidValue("0");
			var7.setHsaChildField(head.getTreeIdField());
			var7.setTableName(CgformSqlUtil.parsePattern(head.getTableName()));
			var7.setTextField(head.getTreeFieldname());
		}

		JSONObject var8 = CgformSqlUtil.a(var5, var6, var7);
		var8.put("table", head.getTableName());
		var3.put("schema", var8);
		var3.put("head", head);
		List var9 = this.queryFormValidButton(var4);
		if (var9 != null && var9.size() > 0) {
			var3.put("cgButtonList", var9);
		}

		if (onlCgformEnhanceJs != null && oConvertUtils.isNotEmpty(onlCgformEnhanceJs.getCgJs())) {
			String var10 = EnhanceJsUtil.c(onlCgformEnhanceJs.getCgJs(), var9);
			onlCgformEnhanceJs.setCgJs(var10);
			var3.put("enhanceJs", EnhanceJsUtil.a(onlCgformEnhanceJs.getCgJs()));
		}

		return var3;
	}

	public JSONObject queryOnlineFormObj(OnlCgformHead head) {
		OnlCgformEnhanceJs var2 = this.onlCgformHeadService.queryEnhanceJs(head.getId(), "form");
		return this.queryOnlineFormObj(head, var2);
	}

	public List<OnlCgformButton> queryFormValidButton(String headId) {
		List<OnlCgformButton> var2 = this.onlCgformHeadService.queryButtonList(headId, false);
		List var3 = null;
		if (var2 != null && var2.size() > 0) {
			LoginUser var4 = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			String var5 = var4.getId();
			List var6 = this.onlAuthPageService.queryFormHideButton(var5, headId);
			var3 = (List) var2.stream().filter((var1) -> {
				return var6 == null || var6.indexOf(var1.getButtonCode()) < 0;
			}).collect(Collectors.toList());
		}

		return var3;
	}

	private List<OnlCgformField> a(String var1) {
		LambdaQueryWrapper<OnlCgformField> var2 = new LambdaQueryWrapper(OnlCgformField.class);
		var2.eq(OnlCgformField::getCgformHeadId, var1);
		var2.orderByAsc(OnlCgformField::getOrderNum);
		return this.onlCgformFieldService.list(var2);
	}

}
